<?php

namespace App\Imports;

use App\Models\Student\Point;
use App\Models\Student\Student;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;

class StudentPointImport implements ToCollection,WithCalculatedFormulas
{

    public $guardUserId;
    public $msg;
    public $success;
    public $count;
    public $countOK;
    public $countFail;

    public function __construct($guardUserId) {
        $this->guardUserId = $guardUserId;
        $this->success = true;
        $this->count = 0;
        $this->countOK = 0;
        $this->countFail = 0;
    }

    /**
    * @param Collection $collection
    */
    public function collection(Collection $records) {

        try{
            DB::beginTransaction();

            $phones = Student::pluck('phone')->toArray();
            $isStudent = [];
            $isNotStudent = [];
            foreach ($records as $k=>$record) {
                $record = $record->toArray();
                if ($k==0) continue;
                if (count(array_filter($record))<=0) continue;
                $temp = [
                    'name'=>$record[0],
                    'phone'=>$record[1],
                    'email'=>$record[2],
                    'college'=>$record[3],
                    'major'=>$record[4],
                    'scholarship'=>$record[5],
                    'budget_fee'=>$record[6],
                    'adjust_college'=>$record[7],
                    'adjust_major'=>$record[8],
                    'schoolarship_du'=>$record[9],
                    'gpa'=>$record[10],
                    'review_point'=>$record[11],
                    'budget_fee_du'=>$record[12],
                    'interview_point'=>$record[13],
                    'from'=>$record[14],
                    'created_at'=>date('Y-m-d h:i:s'),
                    'updated_at'=>date('Y-m-d h:i:s'),
                ];

                $this->count++;


                //$isStudent[] = $temp;
                //$isNotStudent[] = $temp;
                if(in_array($record[1], $phones)){
                    $isStudent[] = $temp;
                }else{
                    unset($temp['created_at']);
                    unset($temp['updated_at']);
                    $temp['reason'] = '当前学员信息没有在报名列表查询到，请确认信息的准确性';
                    $isNotStudent[] = $temp;
                }
            }
            if(count($isStudent)>0){
                foreach ($isStudent as &$item) {
                    $item['guard_user_id'] = $this->guardUserId;
                }
                Point::insert($isStudent);
            }
            if(count($isNotStudent)>0){
                Cache::put('point_'.$this->guardUserId, $isNotStudent,3600);
            }

            $this->countOK = count($isStudent);
            $this->countFail = count($isNotStudent);

            $this->msg = "本次导入记录共：".$this->count."，成功：".$this->countOK."，失败：".$this->countFail;
            DB::commit();
            return true;
        }catch(\Exception $e){
            DB::rollback();
            $this->success = false;
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            Log::error($tip);
            $this->msg = $tip;
        }
    }
}
